// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.patent.service.impl;

import com.liuxinlong.enums.PatentClassTypeEnum;
import com.liuxinlong.enums.ProjectLevelEnum;
import com.liuxinlong.modules.dao.PatentDepartmentDao;
import com.liuxinlong.modules.dao.PatentProposalHistoryDao;
import com.liuxinlong.modules.dao.PatentRiskHistoryDao;
import com.liuxinlong.modules.dao.ProjectDao;
import com.liuxinlong.modules.entity.PatentProposalHistory;
import com.liuxinlong.modules.entity.Project;
import com.liuxinlong.modules.patent.service.PatentProposalHistoryService;
import com.liuxinlong.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风险专利历史实现类
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2024-08-21
 */
@Service
@Slf4j
public class PatentProposalHistoryServiceImpl implements PatentProposalHistoryService {

    @Autowired
    private PatentProposalHistoryDao patentProposalDao;

    @Autowired
    private PatentRiskHistoryDao patentRiskDao;

    @Autowired
    private ProjectDao projectDao;

    @Autowired
    private PatentDepartmentDao patentDepartmentDao;


    @Override
    public List<Map<String, Object>> pagePatentProposalHistoryInfo(Map<String, Object> queryParam) {
        List<PatentProposalHistory> originList = patentProposalDao.pagePatentProposalHistoryList(queryParam);
        return completeResult(originList, (int) queryParam.get("startNum"),
                (String) queryParam.get("projectId"), (String) queryParam.get("processId"));
    }

    @Override
    public int countPatentProposalHistory(Map<String, Object> queryParam) {
        return patentProposalDao.getPatentProposalHistoryCount(queryParam);
    }

    /**
     * 补全数据
     *
     * @param originList 原始数据
     * @param startNum   起始值
     * @return 补全后数据
     */
    private List<Map<String, Object>> completeResult(List<PatentProposalHistory> originList,
                                                     int startNum,String projectId,String processId) {
        List<Map<String, Object>> resultList = new ArrayList<>(originList.size());
        Project project = projectDao.selectById(projectId);
        Map<String,Object> queryParam = new HashMap<>();
        queryParam.put("projectId",projectId);
        queryParam.put("processId",processId);
        Map<String, Map<String, String>> patentRiskMap = patentRiskDao.getPatentRiskMap(queryParam);
        Map<String, Map<String, String>> departmentMap = patentDepartmentDao.getDepartmentMap();
        int sort = startNum + 1;
        for (PatentProposalHistory patentProposal : originList) {
            Map<String, Object> map = ObjectUtils.object2Map(patentProposal);
            map.put("sort", sort);
            map.put("classTypeName", PatentClassTypeEnum.getNameByValue(patentProposal.getClassType()));
            map.put("statusName", patentProposal.getStatus());
            map.put("riskTypeName", patentProposal.getRiskType() == 1 ? "侵权" : "不侵权");
            if (!StringUtils.isEmpty(projectId)) {
                if (!ObjectUtils.isEmpty(project)) {
                    map.put("projectName", project.getProjectName());
                    int projectType = project.getProjectType();
                    map.put("projectTypeName", projectType == 1 ? "预研项目" : projectType == 2 ? "新品开发" : projectType == 3 ? "其他" : "未知类型");
                    map.put("departmentName", departmentMap.containsKey(project.getDepartment()) ? departmentMap.get(project.getDepartment()).get("name") : "");
                    map.put("projectLevelName", ProjectLevelEnum.getNameByValue(project.getProjectLevel()));
                }
            }
            map.put("riskName", StringUtils.isEmpty(patentProposal.getRiskId()) ? null :
                    patentRiskMap.containsKey(patentProposal.getRiskId()) ?
                            patentRiskMap.get(patentProposal.getRiskId()).get("name") : null);
            map.put("levelName", patentProposal.getLevel() == 1 ? "高风险" : patentProposal.getLevel() == 2 ? "低风险" : patentProposal.getLevel() == 3 ? "无风险" : "未定义");
            resultList.add(map);
            sort++;
        }
        return resultList;
    }
}
